﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;

namespace ERP_RORZE_TOOL.DBAccess
{
	class KindDAO : BaseDAO
	{
		#region Basic Method

		public static DataTable GetAll()
		{
			return ExcuteProcedure(ToolConstants.StoreProcedure.Kind.GetAll);
		}

		public static DataTable GetOne(string kindId)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
						CreateParameter("@KindID", SqlDbType.NVarChar, kindId)
                    };
				return ExcuteProcedure(ToolConstants.StoreProcedure.Kind.GetOne, parameters);

			}
			catch(Exception e)
			{
				throw new MyException(e);
			}

		}

		public static Kind LoadOne(string kindId)
		{
			Kind o = new Kind();
			DataTable dt = GetOne(kindId);
			if(dt.Rows.Count > 0)
			{
				o.KindID = dt.Rows[0][ToolConstants.Entities.Kind.FieldName.KindID].ToString();
				o.KindName = dt.Rows[0][ToolConstants.Entities.Kind.FieldName.KindName].ToString();

			}

			return o;
		}


		public static int Insert(Kind o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@KindID", SqlDbType.NVarChar, o.KindID),
                        CreateParameter("@KindName", SqlDbType.NVarChar, o.KindName),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Kind.Insert, parameters);

			}
			catch(Exception e)
			{
				throw new MyException(e);
			}

		}

		public static int Update(Kind o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@TempID", SqlDbType.NVarChar, o.TempId),
                        CreateParameter("@KindID", SqlDbType.NVarChar, o.KindID),
                        CreateParameter("@KindName", SqlDbType.NVarChar, o.KindName),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Kind.Update, parameters);
			}
			catch(Exception e)
			{
				throw new MyException(e);
			}
		}

		public static int Delete(Kind o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@KindID", SqlDbType.NVarChar, o.KindID)
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Kind.Delete, parameters);
			}
			catch(Exception e)
			{
				throw new MyException(e);
			}
		}

		public static DataTable GetTemplateTable()
		{
			DataTable table = new DataTable("333_Kind");
			table.Columns.Add("KindID");
			table.Columns.Add("KindName");

			return table;
		}

		#endregion
	}
}
